/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

package com.huawei.saashousekeeper.config.balancestrategy;

import com.huawei.saashousekeeper.constants.LoadBalanceStrategyEnum;

import javax.sql.DataSource;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 随机算法
 *
 * @author lWX1156935
 * @since 2022/4/22
 */
public class RandomStrategy implements LoadBalanceStrategy {

    @Override
    public LoadBalanceStrategyEnum getStrategyType() {
        return LoadBalanceStrategyEnum.RANDOM;
    }

    @Override
    public DataSource get(List<DataSource> dataSourceList) {
        if (dataSourceList.size() == 1) {
            return dataSourceList.get(0);
        }
        return dataSourceList.get(ThreadLocalRandom.current().nextInt(dataSourceList.size()));
    }
}
